package myJs.cps

import japgolly.scalajs.react.ScalaFnComponent
import myJs.myPkg.reactHookForm._
import org.scalajs.dom.html
import japgolly.scalajs.react.vdom.all._

import scala.scalajs.js
import myJs.Implicits._
import myJs.myPkg.reactBootstrap.{OverlayTrigger, Popover}
import cats.effect._
import japgolly.scalajs.react.util.EffectCatsEffect._

object InputInlineNoFormGroupCp {

  case class Props(
      name: String,
      registerOptions: RegisterOptions = RegisterOptions(),
      onChange:String => SyncIO[Unit]=(v:String)=>SyncIO{},
      value: String = "",
      onFocus: js.UndefOr[html.Input => Unit] = js.undefined,
      inputWidth: Int = 4,
      show: Boolean = true,
      label: String,
      questionInfo: js.UndefOr[String] = js.undefined,
      readOnly: Boolean = false,
      labelWidth: Int = 2
  ) {
    @inline def render: VdomElement = {
      Component.withKey(this.label)(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    val displayStr = if (props.show) "block" else "none"

    div(
      display := displayStr,
      className := s"",
      label(
        className := s"control-label col-sm-${props.labelWidth}",
        s"${props.label}",
        props.questionInfo.map{questionInfo=>
          val popover = Popover(id = "popover-basic")(
            Popover.Header("说明"),
            Popover.Body(
              questionInfo
            )
          ).rawElement
          OverlayTrigger(
            placement = "auto",
            trigger = js.Array("hover", "focus"),
            overlay = popover
          )(
            span(
              a(`type` := "button", i(className := "fa fa-question-circle-o"))
            )
          )
        },
        ":"
      ),
      div(
        className := s"col-sm-${props.inputWidth}",
        PureInputCp
          .Props(
            name = props.name,
            registerOptions = props.registerOptions,
            onChange = props.onChange,
            value = props.value,
            readOnly = props.readOnly
          )
          .render
      )
    )

  }

}
